package b2.widget
{
	import b2.net.*;
	import b2.util.*;
	
	import com.bit101.components.*;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	import flash.media.*;
	
	public class SystemDialog extends Sprite
	{
		private static var instance:SystemDialog;
		//音乐条
		private var musicSlider:HSlider;
		//音效条
		private var effectSlider:HSlider;
		
		public function SystemDialog()
		{
			super();
			var MessageBoxGgr:Class = ResourceHelper.getInstance().getClassByName("MessageBox0");
			var mainSprite:Sprite = new MessageBoxGgr();
			mainSprite.width = 300;
			mainSprite.height = 240;
			addChild(mainSprite);
			//标题
			var titleText:TextField = new TextField();
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.CENTER;
        	var titleFomat:TextFormat = new TextFormat("微软雅黑", 16, 0xFFFFFF);
        	titleFomat.letterSpacing = 10;
			titleText.defaultTextFormat = titleFomat;
			titleText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			titleText.text = "系统设置";
			titleText.x = (this.width - titleText.width) / 2;
			titleText.y = 10;
			addChild(titleText);
			//音乐
			var musicText:TextField = new TextField();
			musicText.selectable = false;
        	musicText.autoSize = TextFieldAutoSize.LEFT;
			musicText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0xFFFF00);
			musicText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			musicText.text = "音乐";
			musicText.x = (this.width - musicText.width) / 2;
			musicText.y = 50;
			addChild(musicText);
			Style.setStyle(Style.LIGHT);
			musicSlider = new HSlider(this, this.width / 2 - 100, 80, onChangeMusicVolume);
			musicSlider.width = 200;
			musicSlider.height = 15;
			musicSlider.value = BSound.getInstance().musicVolume * musicSlider.maximum;
			//音效
			var effectText:TextField = new TextField();
			effectText.selectable = false;
        	effectText.autoSize = TextFieldAutoSize.LEFT;
			effectText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0xFFFF00);
			effectText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			effectText.text = "音效";
			effectText.x = (this.width - effectText.width) / 2;
			effectText.y = 105;
			addChild(effectText);
			effectSlider = new HSlider(this, this.width / 2 - 100, 135, onChangeEffectVolume);
			effectSlider.width = 200;
			effectSlider.height = 15;
			effectSlider.value = BSound.getInstance().effectVolume * effectSlider.maximum;
			//确定按钮
			var ButtonBgr:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr0");
			var buttonOK:BasicButton = new BasicButton(ButtonBgr, "确定");
			buttonOK.x = (this.width - buttonOK.width) / 2;
			buttonOK.y = 180;
			buttonOK.addEventListener(MouseEvent.CLICK, onButtonOKClick);
			addChild(buttonOK);
		}
		
		private function onChangeMusicVolume(event:Event):void
		{
			BSound.getInstance().musicVolume = musicSlider.value / musicSlider.maximum;
		}
		
		private function onChangeEffectVolume(event:Event):void
		{
			BSound.getInstance().effectVolume = effectSlider.value / effectSlider.maximum;
		}
		
		private function onButtonOKClick(event:MouseEvent):void
		{
			ServerHelper.getInstance().saveSystemInfo();
			close();
		}

		public static function getInstance():SystemDialog
		{
			if (!instance)
				instance = new SystemDialog();
			return instance;
		}
		
		public function close():void
		{
			if (this.parent != null)
				this.parent.removeChild(this);
		}
		
	}
}